(function () {
    'use strict';

    angular
        .module('app.dashboard')
        .controller('DashboardController', DashboardController);

    DashboardController.$inject = ['$q', 'dataservice', 'logger', 'UserReferralData', '$state', '$rootScope', '$mdDialog', 'DefaultListsProvider'];
    /* @ngInject */
    function DashboardController($q, dataservice, logger, UserReferralData, $state, $rootScope, $mdDialog, DefaultListsProvider) {
        var vm = this;
        
        vm.messageCount = 0;
        vm.people = [];
        vm.title = 'Dashboard';
        vm.ShowMenu = true;
        vm.UserFullName = $rootScope.UserFullName;
        vm.NewReferrals = '';
        vm.colors = 'altTheme1';
        vm.PracticeStat = 0;
        vm.PracticeRadioChange = PracticeRadioChange;

        vm.RefInStat = 0;
        vm.RefInRadioChange = RefInRadioChange;

        vm.RefOutStat = 0;
        vm.RefOutRadioChange = RefOutRadioChange;

        vm.SectionClick = SectionClick;

        activate();


        function activate() {
            try {
                if ($rootScope.isAuthenticated) {

                    

                    showWait();
                    
                    //google.charts.setOnLoadCallback(drawWeeklyChart);
                    //google.charts.setOnLoadCallback(drawDailyChart);

                    var promise = UserReferralData.GetUserData($rootScope.CurrentLoginID);
                    promise.then(function (response) {
                        vm.UserSections = UserReferralData.GetUserSections();
                        vm.NewReferrals = UserReferralData.GetNewReferrals();
                        vm.InProcessReferrals = UserReferralData.GetOutstandingReferrals();
                        vm.CompletedReferrals = UserReferralData.GetCompletedReferrals();
                        if (UserReferralData.GetWeeklyStats)
                        {
                            drawWeeklyChart();
                        }
                        if (UserReferralData.GetDailyStats) {
                            drawDailyChart(vm.PracticeStat);
                        }
                        if (UserReferralData.GetDailyReferralInStats) {
                            drawRefInChart(vm.RefInStat);
                        }
                        if (UserReferralData.GetDailyReferralOutStats) {
                            drawRefOutChart(vm.RefOutStat);
                        }

                        $mdDialog.hide();
                    });

                    if ($rootScope.DataLoaded == false) {
                        var p1 = DefaultListsProvider.GetDefaultLists().then(function (response) {
                            $rootScope.DataLoaded = true;
                        });
                    }

                    $rootScope.ShowCrumbs = false;

                    

                    

                    
                }
            }
            catch (ex) {

            }
            finally {
                
            }
        }

        function PracticeRadioChange(newValue) {
            drawDailyChart(newValue);
        }

        function RefInRadioChange(newValue) {
            drawRefInChart(newValue);
        }

        function RefOutRadioChange(newValue) {
            drawRefOutChart(newValue);
        }


        function drawWeeklyChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Referral Date');
            data.addColumn('number', 'Referrals');
            data.addRows(UserReferralData.GetWeeklyStats());

            // Instantiate and draw the chart.
            var chart = new google.charts.Bar(document.getElementById('WeeklyStats'));
            
            var options = {
                chart: {
                    title: 'Weekly Referrals'
                },
                chartArea: {
                    backgroundFill: '#eee'
                },
                legend: { position: "none" },
            };

            chart.draw(data, google.charts.Bar.convertOptions(options));

        }

        function drawDailyChart(freq) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Practice');
            data.addColumn('number', 'Referrals');

            var ChartTitle = "";

            if (freq == 0) {
                data.addRows(UserReferralData.GetDailyStats());
                ChartTitle = "Today's Referrals by Practice";
            }
            else if (freq == 1) {
                data.addRows(UserReferralData.GetYesterdayStats());
                ChartTitle = "Yesterday's Referrals by Practice";
            }
            else if (freq == 2) {
                data.addRows(UserReferralData.GetWeeklyPracticeStats());
                ChartTitle = "Current Week Referrals by Practice";
            }
            else if (freq == 3) {
                data.addRows(UserReferralData.GetMonthlyStats());
                ChartTitle = "Current Month Referrals by Practice";
            }
            else if (freq == 4) {
                data.addRows(UserReferralData.GetPreviousMonthlyStats());
                ChartTitle = "Previous Month Referrals by Practice";
            }

            // Instantiate and draw the chart.
            var visualization = new google.visualization.PieChart(document.getElementById('DailyStats'));

            

            var options = {
               title: ChartTitle,
               is3D: true,
               backgroundColor: '#eee',
               
            };

            visualization.draw(data, options);

        }

        function drawRefInChart(freq) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Physician');
            data.addColumn('number', 'Referrals');
            
            var ChartTitle = "";

            if (freq == 0) {
                data.addRows(UserReferralData.GetDailyReferralInStats());
                ChartTitle = "Today's Referral-In By Physician";
            }
            else if (freq == 1) {
                data.addRows(UserReferralData.GetYesterdayReferralInStats());
                ChartTitle = "Yesterdays's Referral-In By Physician";
            }
            else if (freq == 2) {
                data.addRows(UserReferralData.GetWeeklyReferralInStats());
                ChartTitle = "Current Week Referral-In By Physician";
            }
            else if (freq == 3) {
                data.addRows(UserReferralData.GetMonthlyReferralInStats());
                ChartTitle = "Current Month Referral-In By Physician";
            }
            else if (freq == 4) {
                data.addRows(UserReferralData.GetPreviousMonthlyReferralInStats());
                ChartTitle = "Previous Month Referral-In By Physician";
            }

            // Instantiate and draw the chart.
            var visualization = new google.visualization.PieChart(document.getElementById('RefInStats'));



            var options = {
                title: ChartTitle,
                subtitle:"Test",
                is3D: true,
                backgroundColor: '#eee',

            };

            visualization.draw(data, options);

        }

        function drawRefOutChart(freq) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Physician');
            data.addColumn('number', 'Referrals');
            //data.addRows(UserReferralData.GetDailyReferralOutStats());
            var ChartTitle = "";

            if (freq == 0) {
                data.addRows(UserReferralData.GetDailyReferralOutStats());
                ChartTitle = "Today's Referral-Out By Physician";
            }
            else if (freq == 1) {
                data.addRows(UserReferralData.GetYesterdayReferralOutStats());
                ChartTitle = "Yesterdays's Referral-Out By Physician";
            }
            else if (freq == 2) {
                data.addRows(UserReferralData.GetWeeklyReferralOutStats());
                ChartTitle = "Current Week Referral-Out By Physician";
            }
            else if (freq == 3) {
                data.addRows(UserReferralData.GetMonthlyReferralOutStats());
                ChartTitle = "Current Month Referral-Out By Physician";
            }
            else if (freq == 4) {
                data.addRows(UserReferralData.GetPreviousMonthlyReferralOutStats());
                ChartTitle = "Previous Month Referral-Out By Physician";
            }



            // Instantiate and draw the chart.
            var visualization = new google.visualization.PieChart(document.getElementById('RefOutStats'));



            var options = {
                title: ChartTitle,
                is3D: true,
                backgroundColor: '#eee',

            };

            visualization.draw(data, options);

        }


        function SectionClick(category) {
            $state.go('referral', { Category: category })
        }

        function showWait() {
            $mdDialog.show({
                controller: 'LoginController',
                template: '<md-dialog style="background-color:transparent;box-shadow:none">' +
                            '<div layout="row" layout-sm="column" layout-align="center center" aria-label="wait">' +
                                '<md-progress-circular md-mode="indeterminate" class="md-accent"></md-progress-circular>' +
                            '</div>' +
                         '</md-dialog>',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                fullscreen: false
            })
            .then(function (answer) {

            });
        }
    }
})();
